//
//  TL_UserInfoModel.m
//  WXReader
//
//  Created by LL on 2020/12/2.
//  Copyright © 2020 Andrew. All rights reserved.
//

#import "TL_UserInfoModel.h"

#import "NSObject+Utils.h"

@implementation TL_UserInfoModel

static TL_UserInfoModel * _userInfoModel;
+ (TL_UserInfoModel *)shareInstance {
    return [[self alloc] init];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    BOOL __block status = NO;
    dispatch_once(&onceToken, ^{
        _userInfoModel = [super allocWithZone:zone];
        status = YES;
    });
    if (status) {
        
        NSDictionary *modelDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserInfoModelDict"];
        
        if (modelDict) {
            _userInfoModel = [TL_UserInfoModel mj_objectWithKeyValues:modelDict];
        }
        
    }
    
    if (!_userInfoModel) {
        _userInfoModel = [super allocWithZone:zone];
    }
    
    return _userInfoModel;
}

- (id)copyWithZone:(NSZone *)zone {
    return _userInfoModel;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    return _userInfoModel;
}

#pragma mark - Private

+ (NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
    return @{
        @"token" : @"user_token",
        @"vip"   : @"is_vip",
        @"masterRemain" : @"goldRemain",
        @"subRemain" : @"silverRemain",
        @"totalRemain" : @"remain"
    };
}
+ (NSDictionary *)mj_objectClassInArray {
    return @{
        @"headPic" : @"",
        @"doubtfulTime" : @"",
        @"mobile" : @"",
        @"firstPayTime" : @"",
        @"bindTime" : @"",
        @"freeEndTime" : @"",
        @"desc" : @"",
    };
}
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{
        @"desc" : @"description",
    };
}
+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary {
//    _userInfoModel = [super modelWithDictionary:dictionary];
    _userInfoModel = [TL_UserInfoModel mj_objectWithKeyValues:dictionary];
    if (!kObjectIsEmpty(dictionary)) {
        [dictionary writeToFile:self.userInfoPath atomically:YES];
    }
    return _userInfoModel;
}

+ (NSString *)userInfoPath {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"userInfo.plist"];
}

- (void)dealloc {
    NSLog(@"单例对象被释放");
    // 发送通知或其他逻辑
    [[NSNotificationCenter defaultCenter] postNotificationName:@"UserInfoModelDidBecomeNil" object:nil];
}
@end
